package com.system.borrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.model.HttpParams;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.SecondTitle;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class MobileActivity extends BaseActivity implements View.OnClickListener {
    TextView tv_right;
    private EditText et_mobile, et_code;
    private TextView tv_code;
    private TimeCount time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mobile);
        SecondTitle title = new SecondTitle(this);
        title.setTitle("忘记密码", "下一步");
        time = new TimeCount(60000, 1000);
        tv_right = title.tv_right;
        initView();
        initListener();
    }

    private void initView() {
        et_mobile = (EditText) findViewById(R.id.et_mobile);
        et_code = (EditText) findViewById(R.id.et_code);
        tv_code = (TextView) findViewById(R.id.tv_code);
        et_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() > 0){
                    tv_right.setTextColor(getResources().getColor(R.color.home_text_select));
                    tv_right.setEnabled(true);
                }else{
                    tv_right.setTextColor(getResources().getColor(R.color.line_color));
                    tv_right.setEnabled(false);
                }
            }
        });
    }

    private void initListener() {
        tv_right.setOnClickListener(this);
        tv_code.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_right:
                String mobiles = et_mobile.getText().toString().trim();
                String code = et_code.getText().toString().trim();
                if(mobiles.isEmpty()){
                    ToastShow("请输入手机号码!");
                }else{
                    if(code.isEmpty()){
                        ToastShow("请输入验证码");
                    }else{
                        Intent intent = new Intent(this, ForgetPwdActivity.class);
                        intent.putExtra("mobile",mobiles);
                        intent.putExtra("code",code);
                        startActivity(intent);
                        finish();
                    }
                }
                break;
            case R.id.tv_code:
                String mobile = et_mobile.getText().toString().trim();
                if (mobile.isEmpty()) {
                    ToastShow("请输入手机号码!");
                } else {
                    time.start();
                    getCode(mobile);
                }
                break;
        }
    }
    /**
     * 获取验证码
     *
     * @param mobile
     */
    private void getCode(String mobile) {
        HttpParams params = new HttpParams();
        params.put("mobile", mobile);
        params.put("type","3");
        OkHttpUtils.post(URLDefind.SEND_CODE)
                .tag(this)
                .params(params)
                .cacheMode(CacheMode.NO_CACHE)
                .execute(new DialogCallback<String>(this, String.class, null) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject json = new JSONObject(s);
                            int statusCode = json.getInt("statusCode");
                            if (statusCode == 200) {
                                ToastShow("短信已发送,请注意查收");
                            } else {
                                ToastShow(json.getString("message"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            tv_code.setText("获取");
            tv_code.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            tv_code.setClickable(false);//防止重复点击
            tv_code.setText(millisUntilFinished / 1000 + "s");
        }
    }
}
